require('dotenv').config()

import { ApolloServer } from 'apollo-server'
import {
  ApolloServerPluginLandingPageLocalDefault,
  ApolloServerPluginLandingPageProductionDefault
} from 'apollo-server-core'

import { resolvers } from './graphql/resolvers'
import { typeDefs } from './graphql/typeDefs'
import * as mongoose from 'mongoose'

const server = new ApolloServer({
  typeDefs,
  resolvers,
  csrfPrevention: true,
  cors: {
    origin: ['http://localhost:5173']
  },
  plugins: [
    process.env.NODE_ENV === 'production'
      ? ApolloServerPluginLandingPageProductionDefault({
        embed: true,
        graphRef: 'plaid-gufzoj@current'
      })
      : ApolloServerPluginLandingPageLocalDefault({ embed: true })
  ]
})

const start = async () => {
  try {
    await mongoose.connect(String(process.env.DATABASE_URL))

    server.listen().then(({ url }) => {
      console.log(`🚀 Server ready at ${url}`)
    })
  } catch (e) {
    console.log(e)
  }
}

start()
